<?php

class SixPM extends AbstractParser
{
	protected $_url = '';
	protected $_site = 'http://www.6pm.com';

	public function getRow($html)
	{
		$result = array();
		
		$html = $this->_cutSpaces( $html );
//        echo $html; //  exit;

//		<h1 class="title"><a href="/asics" class="brand">ASICS</a><a href="/asics-gel-cumulus-14~1" class="link fn">GEL-Cumulus® 14</a></h1>

		$pattern = "<h1\s+class=\"title\"><a\s+href=\".*\"\s+class=\"brand\">(.*)</a><a\s+href=\".*\"\s+class=\"link(\s+fn)?\">(.*)<\/a><\/h1>";
		$matched = $this->_match( $pattern, $html );
//		print_r($matched); exit;

		if(!empty($matched[1][0]))
			$result['vendor'] = $matched[1][0];
		if(!empty($matched[3][0]))
			$result['title'] = $matched[3][0];

//<div class="description"><ul><li>You can't help but add a little bounce and hop as you stride along effortlessly. It must be the pillowy plush of the ASICS® GEL-Cumulus® 14 underfoot!</li><li>Successor to the GEL-Cumulus 13<em>(no longer available)</em>.</li><li>A running shoe built for the underpronator to the mild overpronator.</li><li>Breathable open mesh upper.</li><li>Durable synthetic upper for targeted support.</li><li>Plush tongue and collar.</li><li>PHF®<em>(Personalized Heel Fit)</em>creates a custom fit as the two layers of memory foam that line the collar mold to your heel.</li><li>Soft, breathable fabric lining offers a great in-shoe feel.</li><li>Removable foam insole supplies light underfoot cushioning.</li><li>I.G.S®<em>(Impact Guidance System)</em>is an ASICS design philosophy that employs linked componentry to enhance the foot's natural gait from heel strike through toe-off.</li><li>Solyte® midsole is lighter than standard EVA and SpEVA® designed by ASICS. Its responsive nature delivers a supremely cushioned ride and makes it highly durable.</li><li>Rearfoot &amp; forefoot Gel® cushioning systems work together to better attenuate impacting shock and allow the foot to move fluidly throughout the gait cycle.</li><li>Space Trusstic System™ mid-foot stabilizer creates a pocket between the Trusstic System unit and the midsole, promoting more controlled midsole deformation, as well as more efficient foot function.</li><li>Guidance Line® technology employs a vertical flex groove that decouples the tooling along the line of progression for maximum stride efficiency.</li><li>DuraSponge® forefoot outsole material is comprised of a blown AHAR®<em>(ASICS High Abrasion Rubber)</em>rubber for superior cushioning and long-lasting wear.</li><li>AHAR is known for its supreme durability and is strategically placed throughout high-wear areas of the outsole.</li><li>Imported.</li><li class="measurements">Measurements:<ul><li>Weight: 12 oz</li></ul></li><li>Product measurements were taken using size 11, width D - Medium. Please note that measurements may vary by size.</li></ul></div>
		$pattern = "<div\s+class=\"description\">(.*)</div>";
		$matched = $this->_match( $pattern, $html );
//		print_r($matched); exit;

		if(!empty($matched[1][0]))
			$result['description'] = $matched[1][0];

		//<li id="video" class="nonThumb"><a href="http://www.6pm.com/media/video/flvplayer/flvplayer-70110.swf?wmode=opaque&amp;allowfullscreen=true&amp;allowscriptaccess=always&amp;stageW=480&amp;stageH=281&amp;showPreviewImage=false&amp;videoUrl=http://www.6pm.com/media/video/8/0/8/8085294.flv&amp;progressBarColor=#98012E&amp;rollOverColor=#98012E&amp;autoPlays=true" class="prDescVideo " data-toggle="modal" title="Watch the video description" target="_blank"><span class="gae-click*Product-Page*PrImage*Video-Load-Thumbnail-Click">Play Video</span></a></li>
		$pattern = "<li\s+id=\"video\"\s+class=\"nonThumb\"><a\s+href=\"(.*)\".*data\-toggle=\"modal\".*>";
		$matched = $this->_match( $pattern, $html );

		if(!empty($matched[1]) && !empty($matched[1][0]))
		{
			$result['video'] = $matched[1][0];
		}


        $pattern = "<a\s+id=\"frontrow-\w+\"\s+data-angle=\"\w+\"\s+href=\"(.*)\"\s+target=\"_blank\">";
		$matched = $this->_match( $pattern, $html );
//		print_r($matched); exit;

		if(!empty($matched[1]) && !empty($matched[1][0]))
        {
            foreach($matched[1] as $url)
            {
                $url = preg_replace('`MULTIVIEW`i', '4x', $url);
                $result['photos'][$url] = $url;
            }
        }

		// Get color and SKU
		$pattern = "zfcUPU\s?\=\s?\'\/product/(\d+)\/color\/(\d+)\'\;?";
		$matched = $this->_match( $pattern, $html );

		if(!empty($matched[1][0]))
			$result['sku'] = (int)$matched[1][0];

		if(!empty($matched[2][0]))
			$result['color_id'] = (int)$matched[2][0];

		// Current color id
		$pattern = "<select\s+id=\"color\"\s+name=\"colorId\"\s+class=\".*\"\>.*\<option\s+value=\"(\d+)\"\s+selected=\"selected\"\>";
		$matched = $this->_match( $pattern, $html );
//		print_r($matched); exit;

		if(!empty($matched[1][0]) && $matched[1][0] > 0)
			$result['color_id'] = (int)$matched[1][0];

		// Sizes
		$pattern = "stockJSON\s?\=\s?(\[.*\])\;";
		$matched = $this->_match( $pattern, $html );

		if(!empty($matched[1][0]))
		{
//			echo $matched[1][0]; exit;
			$sizes = json_decode($matched[1][0]);

			$result['sizes'] = array();
			foreach($sizes as $size)
			{
				if(!empty($size->color) && $size->color == $result['color_id'] && !empty($size->d3) && !empty($size->onHand))
				{
					$result['sizes'][$size->d3] = $size->onHand;
				}
			}
//			print_r($sizes); exit;
		}
//		print_r($matched); exit;

		// Sizes name
		$pattern = "valueIdToNameJSON\s?\=\s?(.*)\;";
		$matched = $this->_match( $pattern, $html );

		if(!empty($matched[1][0]))
		{
//			echo $matched[1][0]; exit;
			$sizesNames = json_decode($matched[1][0], true);

//			print_r($sizesNames); exit;

			$result['sizes_name'] = array();
			foreach($result['sizes'] as $key => $sizeName)
			{
				if(!empty($sizesNames[$key]) && !empty($sizesNames[$key]['value']))
				{
					$result['sizes_name'][$key] = $sizesNames[$key]['value'];
				}
			}
//			print_r($sizes); exit;
		}
//		print_r($matched); exit;
//		print_r($result); exit;


		$pattern = "colorPrices\s?\=\s?(.*)\;";
		$matched = $this->_match( $pattern, $html );

		if(!empty($matched[1][0]))
		{
			$prices = (array)json_decode(str_replace("'",'"', $matched[1][0]));
			foreach($prices as $id => $price)
			{
				if($id == $result['color_id'] && !empty($price->nowInt))
				{
					$result['price'] = $price->nowInt;
					$result['msrp'] = $price->wasInt;
				}
			}
		}

//		print_r($result); exit;

        if(empty($matched[1][0]))
            return $result;

//		print_r($result); exit;

        return $result;
        return $this->_addRow($result);
	}

    public function getNextPage($html)
    {}

    public function getRowset($html)
    {
	    $html = $this->_cutSpaces($html);
//	    echo $html; exit;

	    //<div id="searchResults"></div>
	    $pattern = "<div\s+id=\"searchResults\">(.*)</div>";
	    $matched = $this->_match( $pattern, $html );

	    if(empty($matched[1][0]))
		    return array();

	    $html = $matched[1][0];

//	    print_r($matched); exit;

//<a href="/adidas-outdoor-terrex-softshell-mid-black1-black-core-energy" class="product product-8367865 style-1904473 first r0-0-0" data-style-id="1904473" data-product-id="8367865">
//<img src="http://a2.zassets.com/images/z/1/9/0/4/4/7/1904473-p-LARGE_SEARCH.jpg" alt="adidas Outdoor - Terrex Softshell Mid" class="productImg">
//<span class="brandName">adidas Outdoor</span>
//<span class="productName">Terrex Softshell Mid</span>
//<span class="price-6pm">$89.99</span>
//<span class="discount">(<strong>40% off</strong> MSRP $150.00)</span>
//</a>

	    $pattern = "<a\s+href=\"(.*)\"\s+class=\"product.*\"\s+data-style-id=\"(\d+)\"\s+data-product-id=\"(\d+)\">";
	    $matched = $this->_match( $pattern, $html );
//	    print_r($matched); exit;

	    if(empty($matched[1]))
		    return array();

	    $aUrl = array();
	    foreach($matched[1] as $i => $url)
	    {
//		    if(strlen($url) > 200)
//		    {
//			    echo $url;
//			    exit;
//		    }

		    $aUrl[$this->_site . $url] = $this->_site . $url;
	    }

	    return $aUrl;

	    print_r($matched); exit;


    }

    public function getPages($html)
    {
	    $html = $this->_cutSpaces($html);
//	    echo $html; exit;

	    $pattern = "<div\s+class=\"pagination\">(.*)<\/a>\s*<\/div>";
	    $matched = $this->_match( $pattern, $html );

//	    print_r($matched); exit;

	    if(empty($matched[1][0]))
		    return array();

	    $pattern = "<a\s+href=\"(.*)\".*>(\d+)<\/a>";
	    $matched = $this->_match( $pattern, $matched[1][0] );

//	    print_r($matched); exit;

	    if(empty($matched[1]))
		    return array();

	    $aUrl = array();
	    foreach($matched[1] as $i => $url)
	    {
		    $aUrl[$this->_site . $url] = $this->_site . $url;
	    }

	    return $aUrl;

	    print_r($matched); exit;

    }

    public function getItem($html)
    {}

}